<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page language="java" import="java.lang.*" %>
<%@ page import = "java.sql.SQLException" %>
<%@ page import = "com.seniorproject.aims.*" %>
<%@ page import = "java.util.List" %>
<%@ page import="java.util.ArrayList"%>
<%@ page import = "java.util.Properties" %>
<%@ page import = "javax.servlet.ServletContext" %>
<%@ page import = "java.io.File" %>
<%@ page import = "java.io.InputStream" %>

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Edit Group Info</title>
	
	<link rel="stylesheet" type="text/css" href="css/stylev2.css" />
	<link rel="stylesheet" type="text/css" href="css/student-system.css" />
	<link rel="stylesheet" type="text/css" href="css/frame.css" />
	<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
		<script type="text/javascript" src="js/bootbox.js"></script>
	
	<style type="text/css">
		td {
			height: 50px;
		}
		
		input {
			font-size: 20px;
		}
		
		select {
			font-size: 20px;
		}
		a {
	      text-decoration:none;
	   	}
	</style>
	
<script type="text/javascript">

</script>	
	
</head>
<body>

<%
	//get session
	String strUser = String.valueOf(session.getAttribute("sUser"));
	HttpSession htp_session = request.getSession();
	
	// check session
	if (htp_session == null || htp_session.getAttribute("sUser") == null) {
	    // Forward the control to login.jsp if authentication fails or session expires
	    request.getRequestDispatcher("/login.jsp").forward(request,
	        response);
	}
	
	if("admin".equals(htp_session.getAttribute("sUser"))){%>
	<script>alert("This user dont have PERMISSION to access this zone.");</script>
	<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=admin_home.jsp"> 	
	<%}
	
	//GET PERMISSION

			String name = String.valueOf(session.getAttribute("name"));
			String surname = String.valueOf(session.getAttribute("surname"));
			String role_name = String.valueOf(session.getAttribute("curriculum_role"));
			
			String department_id = String.valueOf(session.getAttribute("department_id"));
					
			System.out.println("name : " + name);///////////////////////////////
			System.out.println("department_id : " + department_id);/////////////////////////
			
			List<String> system_name = new ArrayList<String>();
			
			Permission permission = new Permission(strUser,name);
			permission.setCurriculumCreate(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_create"))));
			permission.setCurriculumDelete(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_delete"))));
			permission.setCurriculumRead(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_read"))));
			permission.setCurriculumUpdate(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_update"))));
			
			String permiss = "";
			if(permission.getCurriculumCreate()) {
				permiss = permiss + "Add";
			}
			if(permission.getCurriculumUpdate()) {
				if("".equals(permiss)) {
					permiss = permiss + "Edit";
				} else {
					permiss = permiss + " Edit";
				}
			}
			if(permission.getCurriculumRead()) {
				if("".equals(permiss)) {
					permiss = permiss + "Read";
				} else {
					permiss = permiss + " Read";
				}
			}
			if(permission.getCurriculumDelete()) {
				if("".equals(permiss)) {
					permiss = permiss + "Delete";
				} else {
					permiss = permiss + " Delete";
				}
			}
			
		///////////////////////////////////////////////////////////////////////////////////////////////////////////			
		//check permission
			if(!permission.getCurriculumUpdate()){%>
				<script>alert("This user dont have PERMISSION to access this zone.");</script>
				<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=curriculum_home.jsp"> 	
			<%}
		
	//set Database Connection
		String hostProps = "";
		String usernameProps  = "";
		String passwordProps  = "";
		String databaseProps = "";
		
		try {
			//get current path
			ServletContext servletContext = request.getSession().getServletContext();
			
			InputStream input = servletContext.getResourceAsStream("/properties/connectDB.properties");
			Properties props = new Properties();
			
			props.load(input);

			hostProps  = props.getProperty("host");
			usernameProps  = props.getProperty("username");
			passwordProps  = props.getProperty("password");
			databaseProps = props.getProperty("database");
		} catch (Exception e) { 
			out.println(e);  
		}
		
		// connect database
		Connection connect = null;		
		try {
			Class.forName("com.mysql.jdbc.Driver");
		
			connect =  DriverManager.getConnection("jdbc:mysql://" + hostProps  + "/" + databaseProps +
					"?user=" + usernameProps  + "&password=" + passwordProps + "&characterEncoding=tis620" );
		
			if(connect != null){
				System.out.println("Database Connect Sucesses.");
			} else {
				System.out.println("Database Connect Failed.");	
			}

		} catch (Exception e) {
			out.println(e.getMessage());
			e.printStackTrace();
		}
		
	
	
	//GET PARAMETE FROM HIDDEN
	String flag = request.getParameter("flag");
	int CurriculumIdOLD = Integer.parseInt(request.getParameter("CurriculumId"));
	int CategoryIdOLD = Integer.parseInt(request.getParameter("CategoryId"));
	String categoryName = new String(request.getParameter("categoryName").getBytes("ISO8859_1"),"UTF-8");
	String GroupNameOLD = new String(request.getParameter("GroupName").getBytes("ISO8859_1"),"UTF-8");	
	int Credit_categoryOLD = Integer.parseInt(request.getParameter("Credit_category"));
	String OHCE = request.getParameter("OHCE");
 	String ONEC = request.getParameter("ONEC");
 	String EngApp = request.getParameter("EngApp");
 	int Credit_group = Integer.parseInt(request.getParameter("Credit_group"));
 	String checkButton = request.getParameter("checkButton");
 	
 	String checker = request.getParameter("checker");
 	
 	System.out.println("2CurriculumIdOLD:   "+CurriculumIdOLD);
	System.out.println("2CategoryIdOLD:   "+CategoryIdOLD);			
	System.out.println("2Credit_categoryOLD:   "+Credit_categoryOLD);		
	System.out.println("2categoryName:    "+GroupNameOLD);
	System.out.println("2OHCE:    "+OHCE);
	System.out.println("2ONEC:    "+ONEC);
	System.out.println("2EngApp:    "+EngApp);
	System.out.println("2checker:    "+checker);
	System.out.println("checkButton:    "+checkButton);
	
	//get parameter
	String GroupId = request.getParameter("GroupId");
	System.out.println("2GroupId:    "+GroupId);
/////////////////////////////////////////////////////////////////////////////////////////////////

		if(checker.equals("SAVE")) {
				System.out.println("SAVE JAAAAAA");
				request.setCharacterEncoding("UTF-8");
				
				String tempGroupName = new String(request.getParameter("GroupNameInfo").getBytes("ISO8859_1"),"UTF-8");
				String tempCreditGroup =request.getParameter("Credit_groupInfo");

				boolean tempCheck = tempCreditGroup.matches("[-+]?\\d+(\\.\\d+)?");
				
			 	if(tempGroupName.equals("") || tempCreditGroup.equals("")){
			 		if(tempGroupName.equals("") ){%>
					<script>alert("กรุณาใส่ชื่อกลุ่มวิชา");</script>
					<%}
						else if(tempCreditGroup.equals("")){%>
						<script>alert("กรุณาใส่จำนวนหน่วยกิต");</script>
					<%}
			 	}if(tempCheck){
					// get value from tag
					String GroupNameNEW = new String(request.getParameter("GroupNameInfo").getBytes("ISO8859_1"),"UTF-8");
					int Credit_groupNEW = Integer.parseInt(request.getParameter("Credit_groupInfo"));
					
					System.out.println("GroupName OLD : "+GroupNameOLD);////////////////////try///////////////////
					System.out.println("GroupName NEW : "+GroupNameNEW);////////////////////try///////////////////
					System.out.println("Credit_group OLD : "+Credit_group);////////////////////try///////////////////
					System.out.println("Credit_group NEW : "+Credit_groupNEW);////////////////////try///////////////////
					
					String test = "UPDATE `subject_group` SET `group_name`='"+ GroupNameNEW + "', "
							+ " `credit`='" + Credit_groupNEW +"'"							
							+ " WHERE group_id="+GroupId+"')";
							
					System.out.println("test : "+test);////////////////////try///////////////////		
					
					try {							
						connect.createStatement().executeUpdate("UPDATE `subject_group` SET `group_name`='"+ GroupNameNEW + "', "
								+ " `credit`='" + Credit_groupNEW +"'"							
								+ " WHERE group_id="+GroupId);
						
						
						/* Log file */
						String log_user = strUser+" edit subject_group";
						Log.writeUserFile(log_user);
						Log.writeUserFile(test);
						
	%>					<input type="hidden" id="counter" name="counter" value="counter" />	
						<script language="javascript"> alert("แก้ไขข้อมูลสำเร็จ");	</script>
						
						
	<%
					} catch (SQLException e) {
						e.printStackTrace();
					}				
				}else{//checker%>
					<script language="javascript"> alert("กรุณาใส่หน่วยกิตด้วยตัวเลขค่ะ");</script>
				<%}
		}

		connect.close();
	%>	


	

	<div id="page-wrap">
			<div id="inside">
				<table border="0">
					<tr style="font-size:24px">
						<td width="600" height="100" style= "background-color: #273b80;border: 0px ;padding: 0px; 0px;" >
							<div id="header_banner-2"></div>
						</td>
						
						<td width="680" height="100" style= "background-color: #273b80;border: 0px;padding: 0px; 0px;" >
							<p align="right"><label><font size="4" color="#ffffff">username : <%=strUser%>&nbsp;&nbsp;</font></label></p>
							<a href="logout.jsp"><p align="right"><label style="cursor: pointer;"><font size="3" color="#ffffff">log out &nbsp;</font></label></p></a>
						</td>
					</tr>
				</table>
        		
        		<div id="main-content-ac">
        					<div class="imageFrame">แก้ไขข้อมูลกลุ่มวิชา</div>
							<br/><br/><br/><br/><br/>
        						<center>        						      						
	        						<table border="0" width="800"> 									
										<tr style="font-size:20px">
											<form id="form" name="form" method="post" action="curriculum_group_edit_info.jsp">  	
											
													<input type="hidden" id="CurriculumId" name="CurriculumId" value="<%=CurriculumIdOLD%>" />
													<%System.out.println("CurriculumIdOLD1 "+CurriculumIdOLD); %>
													<input type="hidden" id="CategoryId" name="CategoryId" value="<%=CategoryIdOLD%>" />
													<input type="hidden" id="categoryName" name="categoryName" value="<%=categoryName%>" />
													<input type="hidden" id="Credit_category" name="Credit_category" value="<%=Credit_categoryOLD%>" />
													<input type="hidden" id="GroupId" name="GroupId" value="<%=GroupId%>" />
													<input type="hidden" id="OHCE" name="OHCE" value="<%=OHCE%>" />
													<input type="hidden" id="ONEC" name="ONEC" value="<%=ONEC%>" />						
													<input type="hidden" id="EngApp" name="EngApp" value="<%=EngApp%>" />
													<input type="hidden" id="flag" name="flag" value="0" />
													<input type="hidden" id="checker" name="checker" value="SAVE" />													
													<input type="hidden" id="counter" name="counter" value="none" />	
																										
													<input type="hidden" id="GroupName" name="GroupName" value="<%=GroupNameOLD%>" /> 
													<input type="hidden" name="Credit_group" id="Credit_group" value="<%=Credit_group%>" />	
													
												<tr>
													<td>
														<label for="labelcategoryName"> <font size="5"><strong>กลุ่มวิชา</strong></font></label>
														<label><font size="5" color="#FF0000">*</font></label>
													</td>
													<td>
														<input type="text" name="GroupNameInfo" id="GroupNameInfo" required value="<%=GroupNameOLD%>" />
														<input type="hidden" id="GroupName" name="GroupName" value="<%=GroupNameOLD%>" /> 
													</td>
												</tr>
												<tr>	
													<td>
														<label for="labelCredit"> <font size="5"><strong>หน่วยกิต</strong></font> </label><label><font size="5" color="#FF0000">*</font></label>
													</td>
													<td>
														<input type="text" name="Credit_groupInfo" id="Credit_groupInfo" required value="<%=Credit_group%>" />  
														<input type="hidden" name="Credit_group" id="Credit_group" value="<%=Credit_group%>" />
													</td>
													
												</tr>
												
										<tr>
										<%
											if(checkButton.equals("Edit")){
										%>		
												<td></td>
												<td><input type="submit" id="btn_edit_info" value="edit" name="edit" class="buttonStyle" /></td>
												<input type="hidden" name="checkButton" id="checkButton" value="Save" />
										<%} %>
											</form>	
										
										
																
											<form id="form_save_info" name="form_save_info" method="post" action="curriculum_edit_group.jsp">  
													
													<input type="hidden" id="GroupId" name="GroupId" value="none" /> 
													<input type="hidden" id="checkerCancel" name="checkerCancel" value="none" /> 
												
													<input type="hidden" id="CurriculumId" name="CurriculumId" value="<%=CurriculumIdOLD%>" />
													<%System.out.println("CurriculumIdOLD2 "+CurriculumIdOLD); %>
													<input type="hidden" id="CategoryId" name="CategoryId" value="<%=CategoryIdOLD%>" />
													<input type="hidden" id="categoryName" name="categoryName" value="<%=categoryName%>" />
													<input type="hidden" id="Credit_category" name="Credit_category" value="<%=Credit_categoryOLD%>" />
													<input type="hidden" id="GroupId" name="GroupId" value="<%=GroupId%>" />
													<input type="hidden" id="OHCE" name="OHCE" value="<%=OHCE%>" />
													<input type="hidden" id="ONEC" name="ONEC" value="<%=ONEC%>" />						
													<input type="hidden" id="EngApp" name="EngApp" value="<%=EngApp%>" />
													<input type="hidden" id="flag" name="flag" value="0" />													
													<input type="hidden" id="GroupName" name="GroupName" value="<%=GroupNameOLD%>" /> 
													<input type="hidden" name="Credit_group" id="Credit_group" value="<%=Credit_group%>" />
													<input type="hidden" id="checker" name="checker" value="none" />
										
										<%
											if(checkButton.equals("Save")){
										%>
													<td></td>
													<td><input type="submit" id="btn_save_info" value="save" name="back" class="buttonStyle"/></td>	
													<input type="hidden" name="checkButton" id="checkButton" value="Edit" />									
											</form>												
										<%
											}
										%>		
											
										</tr>
								</table>
							</center>
							
						<br><br><br><br><br><br><br><br>	
						</div>
					<div style="clear: both;"></div>
						<div id="footer"></div>
			</div>		
						<div style="clear: both;"></div>		
	</div>
		
</body>
</html>